//
//  UIView+Extension.swift
//  Simple Switf Project
//
//  Created by 陈卫东 on 2017/1/17.
//  Copyright © 2017年 陈卫东. All rights reserved.
//

import UIKit

extension UIView {

    public func addDashedBorder(borderColor: CGColor, lineWidth: CGFloat, cornerRadius: CGFloat) {
        let shapeLayer = CAShapeLayer()

        let shapeRect = CGRect(x: 0, y: 0, width: self.width, height: self.height)
//        shapeLayer.bounds = shapeRect
//        shapeLayer.position = CGPoint(x: self.width / 2.0, y: self.height / 2.0)
        shapeLayer.fillColor = nil
        shapeLayer.strokeColor = borderColor
        shapeLayer.lineWidth = lineWidth
        shapeLayer.lineJoin = kCALineJoinRound
        shapeLayer.lineDashPattern = [ 4, 4 ]

        let path = UIBezierPath(roundedRect: shapeRect, cornerRadius: cornerRadius)
        shapeLayer.path = path.cgPath

        self.layer.addSublayer(shapeLayer)
    }

    public var left: CGFloat {
        get {
            return self.frame.origin.x
        }
        set {
            var frame = self.frame
            frame.origin.x = newValue
            self.frame = frame
        }
    }

    public var top: CGFloat {
        get {
            return self.frame.origin.y
        }
        set {
            var frame = self.frame
            frame.origin.y = newValue
            self.frame = frame
        }
    }

    public var right: CGFloat {
        get {
            return self.frame.origin.x + self.frame.size.width
        }
        set {
            var frame = self.frame
            frame.origin.x = newValue - self.frame.size.width
            self.frame = frame
        }
    }

    public var bottom: CGFloat {
        get {
            return self.frame.origin.y + self.frame.size.height
        }
        set {
            var frame = self.frame
            frame.origin.y = newValue - self.frame.size.height
            self.frame = frame
        }
    }

    public var centerX: CGFloat {
        get {
            return self.center.x
        }
        set {
            self.center = CGPoint.init(x: newValue, y: self.center.y)
        }
    }

    public var centerY: CGFloat {
        get {
            return self.center.y
        }
        set {
            self.center = CGPoint.init(x: self.center.x, y: newValue)
        }
    }

    public var width: CGFloat {
        get {
            return self.frame.size.width
        }
        set {
            var frame = self.frame
            frame.size.width = newValue
            self.frame = frame
        }
    }

    public var height: CGFloat {
        get {
            return self.frame.size.height
        }
        set {
            var frame = self.frame
            frame.size.height = newValue
            self.frame = frame
        }
    }

    public var origin: CGPoint {
        get {
            return self.frame.origin
        }
        set {
            var frame = self.frame
            frame.origin = newValue
            self.frame = frame
        }
    }

    public var size: CGSize {
        get {
            return self.frame.size
        }
        set {
            var frame = self.frame
            frame.size = newValue
            self.frame = frame
        }
    }
}

